package CtrLayer;
import java.util.ArrayList;

import DBLayer.DBConnection;
import DBLayer.DBProduct;
import ModelLayer.*;

public class ProductCtr {

	public ProductCtr()
	{
		
	}
	/**
	 * Insert products
	 */
	public void insertCloth(String _name, double _purchasePrice, double _salesPrice,
			double _rentPrice, String _countryOfOrigin, int _minStock, int _stockAmount, Supplier _supplier,
			String _size, String _colour)
	{    
		Clothing cObj = new Clothing();
		cObj.setName(_name);
		cObj.setPurchasePrice(_purchasePrice);
		cObj.setSalesPrice(_salesPrice);
		cObj.setRentPrice(_rentPrice);
		cObj.setCountryOfOrigin(_countryOfOrigin);
		cObj.setMinStock(_minStock);
		cObj.setStockAmount(_stockAmount);
		cObj.setSupplier(_supplier);
		cObj.setSize(_size);
		cObj.setColour(_colour);
		
		try{
			DBConnection.startTransaction();
			DBProduct dbProd = new DBProduct();
			dbProd.insertProduct(cObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	public void insertEquip(String _name, double _purchasePrice, double _salesPrice,
			double _rentPrice, String _countryOfOrigin, int _minStock, int _stockAmount, Supplier _supplier,
			String _type, String _description)
	{    
		Equipment eObj = new Equipment();
		eObj.setName(_name);
		eObj.setPurchasePrice(_purchasePrice);
		eObj.setSalesPrice(_salesPrice);
		eObj.setRentPrice(_rentPrice);
		eObj.setCountryOfOrigin(_countryOfOrigin);
		eObj.setMinStock(_minStock);
		eObj.setStockAmount(_stockAmount);
		eObj.setSupplier(_supplier);
		eObj.setType(_type);
		eObj.setDescription(_description);
		
		try{
			DBConnection.startTransaction();
			DBProduct dbProd = new DBProduct();
			dbProd.insertProduct(eObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	public void insertGunRep(String _name, double _purchasePrice, double _salesPrice,
			double _rentPrice, String _countryOfOrigin, int _minStock, int _stockAmount, Supplier _supplier,
			String _fabric, String _calibre)
	{    
		GunReplicas gObj = new GunReplicas();
		gObj.setName(_name);
		gObj.setPurchasePrice(_purchasePrice);
		gObj.setSalesPrice(_salesPrice);
		gObj.setRentPrice(_rentPrice);
		gObj.setCountryOfOrigin(_countryOfOrigin);
		gObj.setMinStock(_minStock);
		gObj.setStockAmount(_stockAmount);
		gObj.setSupplier(_supplier);
		gObj.setFabric(_fabric);
		gObj.setCalibre(_calibre);
		
		try{
			DBConnection.startTransaction();
			DBProduct dbProd = new DBProduct();
			dbProd.insertProduct(gObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	/**
	 * Search functions
	 * @param name
	 * @return
	 */
	
	public Product findProd(String name)
	{
		DBProduct dbProd = new DBProduct();
		return dbProd.searchProduct(name);
	}
	
	public Product findProd(int id)
	{
		DBProduct dbProd = new DBProduct();
		return dbProd.searchProduct(id);
	}

	public ArrayList<Product> listProducts()
	{
		DBProduct dbProd = new DBProduct();
		ArrayList<Product> allProd = new ArrayList<Product>();
		allProd = dbProd.getAllProducts();
		return allProd;
	}
	public ArrayList<Product> listProducts(String name)
	{
		DBProduct dbProd = new DBProduct();
		ArrayList<Product> allProd = new ArrayList<Product>();
		allProd = dbProd.getAllProducts(name);
		return allProd;
	}
	/**
	 * Update clothing
	 */
	
	public int updateCloth(int _productId, String _name, double _purchasePrice, double _salesPrice,
			double _rentPrice, String _countryOfOrigin, int _minStock, int _stockAmount, Supplier _supplier,
			String _size, String _colour)
	{
		DBProduct dbProd = new DBProduct();
		Clothing cObj = new Clothing();
		cObj.setProductId(_productId);
		cObj.setName(_name);
		cObj.setPurchasePrice(_purchasePrice);
		cObj.setSalesPrice(_salesPrice);
		cObj.setRentPrice(_rentPrice);
		cObj.setCountryOfOrigin(_countryOfOrigin);
		cObj.setMinStock(_minStock);
		cObj.setStockAmount(_stockAmount);
		cObj.setSupplier(_supplier);
		cObj.setSize(_size);
		cObj.setColour(_colour);
		return  dbProd.updateProduct(cObj);
	}
	
	public int updateEquip(int _productId, String _name, double _purchasePrice, double _salesPrice,
			double _rentPrice, String _countryOfOrigin, int _minStock, int _stockAmount, Supplier _supplier,
			String _type, String _description)
	{
		DBProduct dbProd = new DBProduct();
		Equipment eObj = new Equipment();
		eObj.setProductId(_productId);
		eObj.setName(_name);
		eObj.setPurchasePrice(_purchasePrice);
		eObj.setSalesPrice(_salesPrice);
		eObj.setRentPrice(_rentPrice);
		eObj.setCountryOfOrigin(_countryOfOrigin);
		eObj.setMinStock(_minStock);
		eObj.setStockAmount(_stockAmount);
		eObj.setSupplier(_supplier);
		eObj.setType(_type);
		eObj.setDescription(_description);
		return  dbProd.updateProduct(eObj);
	}
	
	public int updateGunRep(int _productId, String _name, double _purchasePrice, double _salesPrice,
			double _rentPrice, String _countryOfOrigin, int _minStock, int _stockAmount, Supplier _supplier,
			String _fabric, String _calibre)
	{
		DBProduct dbProd = new DBProduct();
		GunReplicas gObj = new GunReplicas();
		gObj.setProductId(_productId);
		gObj.setName(_name);
		gObj.setPurchasePrice(_purchasePrice);
		gObj.setSalesPrice(_salesPrice);
		gObj.setRentPrice(_rentPrice);
		gObj.setCountryOfOrigin(_countryOfOrigin);
		gObj.setMinStock(_minStock);
		gObj.setStockAmount(_stockAmount);
		gObj.setSupplier(_supplier);
		gObj.setFabric(_fabric);
		gObj.setCalibre(_calibre);
		return  dbProd.updateProduct(gObj);
	}
	
	/**
	 * Remove product by id.
	 * @param id
	 */


	public void removeProd(int id) {
		DBProduct dbProd = new DBProduct();
		Product ProdObj = new Product();
		ProdObj = dbProd.searchProduct(id);
		
		try{
			DBConnection.startTransaction();
			dbProd.deleteProduct(ProdObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	
	





}
